package com.zhenmyan.secdeal.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * description:
 *
 * @author 颜真明
 * @date 2023/5/15  10:31
 */
@Service
@Slf4j
public class MQHeadersSender {

    private static final String HEADERS_EXCHANGE = "headers_exchange";

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 这里满足第二个队列的要求，只有一个队列接收
     *
     * @param message
     */
    public void send1(String message) {
        log.info("headers模式发送消息（只有队列2接收）:" + message);
        MessageProperties properties = new MessageProperties();
        properties.setHeader("color", "red");
        properties.setHeader("speed","low");
        Message mes = new Message(message.getBytes(), properties);
        rabbitTemplate.convertAndSend(HEADERS_EXCHANGE, "", mes);
    }

    /**
     * 这里满足两个队列的要求，会有两个队列接收
     *
     * @param message
     */
    public void send2(String message) {
        log.info("headers模式发送消息（队列1、2都可接收）:" + message);
        MessageProperties properties = new MessageProperties();
        properties.setHeader("color", "red");
        properties.setHeader("speed","fast");
        Message mes = new Message(message.getBytes(), properties);
        rabbitTemplate.convertAndSend(HEADERS_EXCHANGE, "", mes);
    }

}
